package org.fly.service.impl;

import org.fly.bean.ProductNorm;
import org.fly.bean.query.ProductNormQuery;
import org.fly.dao.ProductNormDao;
import org.fly.service.ProductNormService;
import org.fly.utils.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.fly.bean.common.BusinessException;

import java.util.ArrayList;
import java.util.List;

/**
* Created by autoCode on 2018-03-07 09:00:48.
*/
@Service
public class ProductNormServiceImpl implements ProductNormService {

    @Autowired
    ProductNormDao productNormDao;

    @Override
    public List<ProductNorm> list(ProductNormQuery query) {
        return productNormDao.list(query);
    }

    @Override
    public long count(ProductNormQuery query) {
        return productNormDao.count(query);
    }

    @Override
    public ProductNorm findById(String id) {
        return productNormDao.findById(id);
    }

    @Override
    public ProductNorm findByProductId(String productId) {
        List<String> productIdList = new ArrayList<>();
        productIdList.add(productId);
        List<ProductNorm> normList = productNormDao.listByProductIds(productIdList);
        if(normList != null && normList.size() ==1){
            return normList.get(0);
        }
        return null;
    }

    @Override
    public void save(ProductNorm bean)  throws BusinessException {

    if(StringUtils.isNotBlank(bean.getId())){
         productNormDao.update(bean);
    }else {
        bean.setId(UUIDFactory.random());
        bean.setCreateTime(System.currentTimeMillis());
        productNormDao.insert(bean);
    }
    }

    @Override
    public void delete(String id) {
        productNormDao.delete(id);
    }
}
